import bb.cascades 1.0

Page {
    id: add_detail
    property string mode
    property variant insertmap
    
    function setModeIncome() {
        console.log("setModeIncome");
        mode = "income";
        ctn_income.show();
    }
    
    function setModeExpenses() {
        console.log("setModeExpenses");
        mode = "expenses";
        ctn_expenses.show();
    }
    
    paneProperties: NavigationPaneProperties {
        backButton: ActionItem {
            onTriggered: {
                // define what happens when back button is pressed here
                // in this case is closed the detail page
                addNavigation.pop()
            }
        }
    }
    
    actions: [
        // define the actions for first tab here
//        ActionItem {
//            title: qsTr("Rotate")
//            onTriggered: {
//                //                imgTab1.rotationZ = imgTab1.rotationZ + 90;
//            }
//        },
//        ActionItem {
//            title: qsTr("Break")
//            onTriggered: {
//                //                imgTab1.imageSource = "asset:///images/picture1br.png";
//            }
//        }
    ]
    
    titleBar: TitleBar {
        id: titleBar
        visibility: ChromeVisibility.Visible
        title: lang.add_title
    }
    
    Container {
        //        Container {
        //            Label {
        //                text: lang.add_title
        //            }
        //        }
        
//        Divider {
//        }
        
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            topPadding: 50
            
            Label {
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 1
                }
                verticalAlignment: VerticalAlignment.Center
                text: lang.add_detail_amount
            }
            
            TextField {
                id: input_amount
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 3
                }
                verticalAlignment: VerticalAlignment.Center
                maxWidth: 600
                
                hintText: lang.add_detail_amounthint
                inputMode: TextFieldInputMode.NumbersAndPunctuation
            }
        }
        
        // type of income           
        Container {
            id: ctn_income
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            topPadding: 50
            maxHeight: 0
            
            function show()
                {
                                if (maxHeight == 0)
                maxHeight = 99999 ;
                }
                
                function hide()
                {
                                if (maxHeight > 0)
                maxHeight = 0 ;
                }
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    multiline: true
                    verticalAlignment: VerticalAlignment.Center
                    text: lang.chart_income_type //chart_income_type or chart_expenses_type
                }
                
                DropDown {
                    id: dd_imcome_type
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 3
                    }
                    maxWidth: 600
                    
                    //                    selectedOption: option_income_all
                    //                    Option {
                    //                        id: option_income_all
                    //                        text : lang.type_all
                    //                        description : "Sets the alarm to 2 minutes"
                    //                        value : "type_all"
                    //                    
                    //                    }
                    Option {
                        id: option2
                        text : lang.income_salary
                        //                        description : "Sets the alarm to 1 minutes"
                        value : 1
                    
                    }
                    
                    Option {
                        text : lang.income_investment
                        //                        description : "Sets the alarm to 2 minutes"
                        value : 2
                    
                    }
                    Option {
                        text : lang.income_others
//                        description : "Sets the alarm to 3 minutes"
                        value : 3
                    
                    }
                }
        }
        
        // type of expenses            
        Container {
            id: ctn_expenses
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            topPadding: 50
            maxHeight: 0
            
            function show(){
                if (maxHeight == 0)
maxHeight = 99999 ;
            }
            
            function hide(){
                if (maxHeight > 0)
maxHeight = 0 ;
            }
            
            Label {
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 1
                }
                multiline: true
                verticalAlignment: VerticalAlignment.Center
                text: lang.chart_expenses_type //chart_income_type or chart_expenses_type
            }
            
            DropDown {
                id: dd_expenses_type
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 3
                }
                maxWidth: 600
                
                //                                    selectedOption: option_expense_all
                //                                    Option {
                //                                        id: option_expense_all
                //                                        text : lang.type_all
                //                                        description : "Sets the alarm to 2 minutes"
                //                                        value : "type_all"
                //                                    
                //                                    }
                Option {
                    //                                        id: option4
                    text : lang.expenses_food
                    //                                        description : "Sets the alarm to 1 minutes"
                    value : 1
                
                }
                
                Option {
                    text : lang.expenses_living
                    //                                        description : "Sets the alarm to 2 minutes"
                    value : 2
                
                }
                Option {
                    text : lang.expenses_entertainment
                    //                                        description : "Sets the alarm to 3 minutes"
                    value : 3
                
                }
                Option {
                    //                                        id: option7
                    text : lang.expenses_investment
                    //                                        description : "Sets the alarm to 1 minutes"
                    value : 4
                
                }
                
                Option {
                    text : lang.expenses_travel
                    //                                        description : "Sets the alarm to 2 minutes"
                    value : 5
                
                }
                Option {
                    text : lang.expenses_others
                    //                                        description : "Sets the alarm to 3 minutes"
                    value : 6
                
                }
            }
        }
        
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            topPadding: 50
            
            Label {
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 1
                }
                verticalAlignment: VerticalAlignment.Center
                text: lang.add_detail_time
            }
            
            DateTimePicker {
                id: datepicker
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 3
                }
                
                maxWidth: 600
            }
        }
        
        Container {
            topPadding: 50
            
            TextArea {
                id: input_description
                hintText: lang.add_detail_description
                inputMode: TextAreaInputMode.Text
            }
        }
        
        Container {
            horizontalAlignment: HorizontalAlignment.Center
            topPadding: 50
            
            Button {
                text: lang.add_detail_confirm
                
                onClicked:{
//                         console.log("dd_expenses_type.selectedValue : " + dd_expenses_type.selectedValue);
                    
                    if (Number(input_amount.text) == NaN || Number(input_amount.text) == undefined || input_amount.text == ""){
                        root.showmsg(lang.error_input);
//                    ;
                    }else if (add_detail.mode == "income" && dd_imcome_type.selectedValue == undefined){
                        root.showmsg(lang.error_input);
//                    ;
                    } else if (add_detail.mode == "expenses" && dd_expenses_type.selectedValue == undefined){
                        root.showmsg(lang.error_input);
//                    ;
                    }else {
//                        console.log("000");
//                        var insertmap;
                        var today = new Date();
                        var year = datepicker.value.getFullYear();
                        var month = datepicker.value.getMonth()+1;
                        var day = datepicker.value.getDate();
//                        console.log("111");
                        if (add_detail.mode == "income"){
//                            console.log("222");
                            add_detail.insertmap = {
                                "FNUMBER": today.getTime().toString()
                            ,"FAMOUNT": Number(input_amount.text)
                            ,"FITYPEID": dd_imcome_type.selectedValue
                            ,"FYEAR": year
                            ,"FMONTH": month
                            ,"FDAY": day
                            ,"FDESCRIPTION": input_description.text
                            ,"FDate": year*10000 + month*100 + day
                            ,"FUSERID": 0
                            };
                            var insert_rt = database.Insert("t_bas_income", insertmap);
                            if (insert_rt > 0){
                                root.showmsg(lang.add_detail_successful);
                                addNavigation.pop();
                            }else {
                                root.showmsg(lang.error_database);
                            }
//                            var insert_rt = database.Insert("t_bas_income", {"aa": 1, "bb": 2});
//                            console.log("test db : " + database.test());
                            console.log("insert debug : " + insert_rt);
                        }else if (add_detail.mode == "expenses") {
                            add_detail.insertmap = {
                                "FNUMBER": today.getTime().toString()
                            ,"FAMOUNT": Number(input_amount.text)
                            ,"FPTYPEID": dd_expenses_type.selectedValue
                            ,"FYEAR": year
                            ,"FMONTH": month
                            ,"FDAY": day
                            ,"FDESCRIPTION": input_description.text
                            ,"FDATE": year*10000 + month*100 + day
                            ,"FUSERID": 0
                            };
                            
                            var insert_rt = database.Insert("t_bas_payment", insertmap);
                            if (insert_rt > 0){
                                root.showmsg(lang.add_detail_successful);
                                addNavigation.pop();
                            }else {
                                root.showmsg(lang.error_database);
                            }
                            console.log("insert debug : " + insert_rt);
                        }
                    }
                }
            }
        }
    }
}
